import 'package:beautyflutter/api/request.dart';
import 'package:beautyflutter/api/route.dart';
import 'package:beautyflutter/pages/news/news_list.dart';
import 'package:flutter/material.dart';

import '../../bean/culture_type.dart';
class RequestData extends StatefulWidget {
  @override
  _RequestDataState createState() => _RequestDataState();
}

class _RequestDataState extends State<RequestData> {
  List<NewsCultureTypeList> _selectChannels;
  @override
  void initState() {
    super.initState();
    _selectChannels = new List<NewsCultureTypeList>();
    getData();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('新闻页')),
      body: DefaultTabController(
        length: _selectChannels.length,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.blue,
              child: TabBar(
                labelColor: Colors.red,
                unselectedLabelColor: Colors.white,
                indicatorWeight: 1,
                isScrollable: true,
                // labelPadding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                labelStyle: TextStyle(fontSize: 18),
                tabs: parseTabs(),
              ),
            ),
            Expanded(
              flex: 1,
              child: TabBarView(
                  children: parsePages()
              ),
            )
          ],
        ),
      )
    );
  }

  void getData() {
    Map<String, String> map = new Map();
    map["userId"] = "16990";
    HttpController.post(RouteUtil.getNewsCultureTypeList, (data) {
      if (data != null) {
        CultureType autogenerated = CultureType.fromJson(data);
        List<NewsCultureTypeList> newsCultureTypeList = autogenerated.newsCultureTypeList;
        setState(() {
          _selectChannels = newsCultureTypeList;
        });
      }
    }, params: map);
  }

  List<Widget> parseTabs(){
    List<Widget> tabs = List();
    _selectChannels.forEach((item){
      var tab = Tab(
        child: Text(
          '${item.typeName}'
        ),
      );
      tabs.add(tab);
    });
    return tabs;
  }

  List<Widget> parsePages(){
    List<NewsList> pages = List();
    _selectChannels.forEach((item){
      pages.add(NewsList(newsCultureTypeId: item.newsCultureTypeId));
    });
    return pages;
  }
}